package qx.hyj.petrescue.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import qx.hyj.petrescue.intercept.TokenIntercept;

/**
 * 拦截器配置
 *
 * @author liuShuai
 */
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    @Bean
    public TokenIntercept tokenInterceptor() {
        return new TokenIntercept();
    }

    @Override
    // 重写addInterceptors方法，用于添加拦截器
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(tokenInterceptor())
                .order(1)  // 设置较低的优先级
                .addPathPatterns("/Petrescue/**")
                .excludePathPatterns(
                        "/Petrescue/users/login",
                        "/Petrescue/users/addUser",
                        "/Petrescue/images/**",
                        "/Petrescue/blog//Blogs",
                        "/swagger-resources/**",
                        "/webjars/**",
                        "/v2/api-docs",
                        "/images/**",
                        "/swagger-ui/**",
                        "/swagger-ui.html",
                        "/swagger-ui/index.html",
                        "/doc.html",
                        "/v3/api-docs/**", // 必须包含子路
                        "/favicon.ico",
                        "/csrf",  // 如果使用Spring Security
                        "/error"   // 排除错误页面
                );
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        registry.addResourceHandler("/swagger-ui/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/");
    }
}